from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import viewsets, mixins

from rest_framework.views import APIView

# CBV
from goods.filters import GoodsFilter
from goods.models import MainWheel, MainNav, MainMustBuy, MainShop, MainShow, FoodType, Goods
from goods.serializers import MainWheelSerialzier, MainNavSerialzier, MainMustBuySerializer, MainShopSerializer, \
    MainShowSerializer, FoodTypeSerializer, GoodsSerialzier


class Home(APIView):
    def get(self):
        pass
    def post(self):
        pass

# FBV
@api_view(['GET'])
def home(request):
    main_wheels = MainWheel.objects.all()
    main_navs = MainNav.objects.all()
    main_mustbuys = MainMustBuy.objects.all()
    main_shops = MainShop.objects.all()
    main_shows = MainShow.objects.all()
    res = {
        'main_wheels': MainWheelSerialzier(main_wheels, many=True).data,
        'main_navs': MainNavSerialzier(main_navs, many=True).data,
        'main_mustbuys': MainMustBuySerializer(main_mustbuys, many=True).data,
        'main_shops': MainShopSerializer(main_shops, many=True).data,
        'main_shows': MainShowSerializer(main_shows, many=True).data
    }
    return Response(res)


class FoodTypeView(viewsets.GenericViewSet,
                   mixins.ListModelMixin):

    queryset = FoodType.objects.all()

    serializer_class = FoodTypeSerializer


class MarketView(viewsets.GenericViewSet,
                 mixins.ListModelMixin):

    queryset = Goods.objects.all()

    serializer_class = GoodsSerialzier
    # 过滤类
    filter_class = GoodsFilter

    def list(self, request, *args, **kwargs):
        # filter_queryset调用过滤类进行参数过滤
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)

        # 子分类[{’child_name‘:’进口水果‘， ’child_value‘: 103533}， {}....]
        typeid = request.query_params.get('typeid')
        foodtype = FoodType.objects.filter(typeid=typeid).first()
        # childtypenames: 全部分类:0#酸奶乳酸菌:103537#牛奶豆浆:103538#面包蛋糕:103540
        # 第一种方式:
        # typenames = []
        # for i in foodtype.childtypenames.split('#'):
        #     data = {
        #         'child_name': i.split(':')[0],
        #         'child_value': i.split(':')[1]
        #     }
        #     typenames.append(data)
        # 第二种方式
        typenames = [{
                        'child_name': i.split(':')[0],
                        'child_value': i.split(':')[1]
                     }
                     for i in foodtype.childtypenames.split('#')
                     ]

        # 排序
        rule_list = [
            {'order_name': '价格升序', 'order_value': 1},
            {'order_name': '价格降序', 'order_value': 2},
            {'order_name': '销量升序', 'order_value': 3},
            {'order_name': '销量降序', 'order_value': 4},
        ]
        res = {
            'goods_list': serializer.data,
            'order_rule_list': rule_list,
            'foodtype_childname_list': typenames
        }
        return Response(res)
